package com.zidian.study.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.adapter.PrizeRecordAdapter;
import com.zidian.study.bean.PrizeRecordBean;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by GongCheng on 2016/12/16.
 */

public class PrizeRecordActivity extends ToolBarActivity {
    @Bind(R.id.no_prize_layout)
    LinearLayout noPrizeLayout;
    @Bind(R.id.prize_recycler_view)
    RecyclerView prizeRecyclerView;

    private PrizeRecordAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_prize_record);
        ButterKnife.bind(this);
        setTitle("中奖记录");
        adapter = new PrizeRecordAdapter();
        prizeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        prizeRecyclerView.setAdapter(adapter);
        getPrizeRecord();
    }

    private void getPrizeRecord() {
        Map<String, String> map = new HashMap<>();
        map.put("studentId", GetUserInfo.getStudentId(this));
        map.put("token", GetUserInfo.getToken(this));
        String url = Constant.URL_GOAL + "winnersPrize/sel";
        OkHttpUtils.getInstance()
                .post()
                .params(map)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Snackbar.make(prizeRecyclerView, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                        noPrizeLayout.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.getInt("code")!= 200) {
                                noPrizeLayout.setVisibility(View.VISIBLE);
                                Snackbar.make(prizeRecyclerView, jsonObject.getString("message"), Snackbar.LENGTH_SHORT).show();
                            } else {
                                Gson gson = new Gson();
                                PrizeRecordBean bean = gson.fromJson(response, PrizeRecordBean.class);
                                adapter.setData(bean);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
